﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WaterSetting
{
    public partial class Form1 : Form
    {

        private SerialPort serialPort = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // 获取当前可用的串口列表
            string[] ports = SerialPort.GetPortNames();

            // 打印可用的串口列表
            Console.WriteLine("可用的串口列表:");
            foreach (string port in ports)
            {
                Console.WriteLine(port);
                cmbSerial.Items.Add(port);
            }
            if(ports.Length > 0)
            {
                cmbSerial.Text = cmbSerial.Items[0].ToString();
            }
            txtPort.Text = "2325";
            cmdBoTe.Text = "9600";
            txtIp.Text = "api.sdyuedou.com";
            txtHeartInteval.Text = "60";


        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (serialPort != null)
            {
                if (txtTestData.Text.Trim() == "")
                {
                    MessageBox.Show("请输入测试串口数据");
                    return;
                }
                // 向串口发送数据
                serialPort.Write(txtTestData.Text.Trim());
            }
            else
            {
                MessageBox.Show("请开启串口");
                return;
            }
        }

        private void btnClosePort_Click(object sender, EventArgs e)
        {
            if(serialPort != null)
            {
                btnClosePort.Text = "开启串口";
                // 关闭串口
                serialPort.Close();
            }
            else
            {
                if (cmbSerial.Text.Trim() == "")
                {
                    MessageBox.Show("请选择串口");
                    return;
                }
                if (txtPort.Text.Trim() == "")
                {
                    MessageBox.Show("请输入端口");
                    return;
                }
                if (cmdBoTe.Text.Trim() == "")
                {
                    MessageBox.Show("请选择波特率");
                    return;
                }
                // 提示用户选择一个串口
                Console.Write("请选择一个串口: ");
                string selectedPort = cmbSerial.Text;

                int bote = Convert.ToInt32(cmdBoTe.Text);
                // 创建一个SerialPort对象
                serialPort = new SerialPort(selectedPort, bote, Parity.None, 8, StopBits.One);

                try
                {
                    // 打开串口
                    serialPort.Open();

                    btnClosePort.Text = "关闭串口";
                    btnSubmit.Enabled = true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("发生错误: " + ex.Message);
                }

            }
        }
    }
}
